"""
Case Type   : GUC
Case Name   : instr_unique_sql_track_type
Create At   : 2021/1/25
Owner       : opentestcase018
Description :
    1、修改instr_unique_sql_track_type为all，校验其预期结果；
    gs_guc set -D {cluster/dn1} -c "instr_unique_sql_track_type=all";
    3、恢复默认值；
Expect      :
    1、参数修改成功；
    2、恢复默认值；
History     :
    Modified by li-xin12345 at 2023/10/25:instr_unique_sql_track_type
    现可支持修改，将此用例优化为成功修改参数值为all
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Guctestcase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.default_param = self.common.show_param(
            'instr_unique_sql_track_type')
        self.log.info(self.default_param)

    def test_guc(self):
        text = '-----step1:修改instr_unique_sql_track_type为all，' \
               '校验其预期结果；expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc("set",
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        'instr_unique_sql_track_type=all')
        self.log.info(res)
        self.assertTrue(res)
        restart_res = self.pri_sh.restart_db_cluster()
        self.log.info(restart_res)
        self.assertTrue(restart_res, "执行失败" + text)

    def tearDown(self):
        text = '-----step2:恢复默认值;expect:成功-----'
        self.log.info(text)
        curr_param = self.common.show_param('instr_unique_sql_track_type')
        self.log.info(curr_param)
        if curr_param != self.default_param:
            res = self.pri_sh.execute_gsguc(
                "reload", self.constant.GSGUC_SUCCESS_MSG,
                f"instr_unique_sql_track_type={self.default_param}")
            self.log.info(res)
            self.assertTrue(res)
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
